<?php

function download($url) {
    $file = read_url($url);
    if (!$file) {
        log_message('error','Unable to open URL: '.$url);
        return false;
    }

    $tmpFileName = tempnam(DIRPATH_TEMP,'pfm');

    $xmlFile = write_file($tmpFileName,$file);

    if (!$xmlFile) {
        log_message('error','Unable to open file: '.$xmlFile);
        return false;
    }

    log_message('info','Url ('.$url.') downloaded to file ('.$tmpFileName.')');
    return $tmpFileName;
}

function downloadOwnAccountWith($userName, $password) {
    $accountURL = 'http://www.freekick.org/public/getdata.php?getdata=account'
            .'&datatype=xml&username='.$userName.'&password='.decrypt($password);

    return download($accountURL);
}

function downloadOwnClubWith($userName,$password) {
    $clubURL = 'http://www.freekick.org/public/getdata.php?getdata=ownclub&datatype=xml'.
            '&username='.$userName.'&password='.decrypt($password);

    return download($clubURL);
}

function downloadOwnPlayersWith($userName,$password) {
    $playersURL = 'http://www.freekick.org/public/getdata.php?getdata=ownplayers&datatype=xml'.
            '&username='.$userName.'&password='.decrypt($password);

    return download($playersURL);
}

function downloadMatchReport($matchId) {
    $matchReportURL = 'http://www.freekick.org/public/getdata.php?getdata=matchreport&datatype=xml'.
            '&id='.$matchId;

    return download($matchReportURL);
}

function downloadLanguages() {
    $langsURL = 'http://www.freekick.org/public/getdata.php?getdata=languages&datatype=xml';

    return download($langsURL);
}

function downloadTranslation($lang) {
    $transURL='http://www.freekick.org/public/getdata.php?getdata=translation&datatype=xml'.
            '&lang='.$lang;

    return download($transURL);
}

function downloadPlayers($clubId) {
    $playersURL='http://www.freekick.org/public/getdata.php?getdata=players&datatype=xml'.
            '&clubid='.$clubId;

    return download($playersURL);
}

function downloadMatches($teamId) {
    $matchesURL='http://www.freekick.org/public/getdata.php?getdata=matches&datatype=xml'.
            '&teamid='.$teamId;

    return download($matchesURL);
}

function downloadLeague($league,$division,$series,$squad,$season) {
    $leagueURL='http://www.freekick.org/public/getdata.php?getdata=league&datatype=xml'.
            '&league='.$league.'&division='.$division.'&series='.$series.'&squad='.$squad.'&season='.$season;

    return download($leagueURL);
}

function loadXMLFromFile($fileName,$unlink = FALSE) {
    $doc = new DOMDocument('1.0', 'utf-8');

    $tFile = read_file($fileName);
    if (!$tFile) {
        log_message('error','Unable to open file: '.$fileName);
        return FALSE;
    }

    $tDocument = $doc->loadXML($tFile);
    if (!$tDocument) {
        log_message('error','The file is not valid XML!');
        return FALSE;
    }

    if ($unlink) {
        unlink($fileName);
    }
    return $doc;
}

function parseXML(DOMDocument $doc, $root,$paths) {
    $xPath = new DOMXPath($doc);
    $qResult = $xPath->query($root);
    $data = array();

    if ($qResult->item(0) === NULL) {
        return FALSE;
    }
    $rootElement = $qResult->item(0);
    foreach($paths as $path) {
        $qResult = $xPath->query($path['xpath'],$rootElement);
        if ($qResult->item(0) !== NULL) {
            $data[$path['colName']] = $qResult->item(0)->nodeValue;
        }
    }
    return $data;
}

function parseXMLFile($schema, $fileName, $unlink = FALSE) {
    $fileDoc = loadXMLFromFile($fileName,$unlink);
    $schemaDoc = loadXMLFromFile($schema,FALSE);

    return parseXMLDoc($schemaDoc, $fileDoc);
}

function parseXMLDoc(DOMDocument $schemaDoc, DOMDocument $fileDoc) {
    $schemaXPath = new DOMXPath($schemaDoc);

    $qResult = $schemaXPath->query(SCHEMA_XPATH_ROOT);
    $root = $qResult->item(0)->nodeValue;

    $paths = array();
    $qResult = $schemaXPath->query(SCHEMA_XPATH_PATHS);

    foreach($qResult as $result) {
        $tResult = $schemaXPath->query(SCHEMA_XPATH_COLNAME,$result);
        $colName = $tResult->item(0)->nodeValue;

        $tResult = $schemaXPath->query(SCHEMA_XPATH_XPATH,$result);
        $tXPath = $tResult->item(0)->nodeValue;

        array_push($paths, array('xpath' => $tXPath, 'colName' => $colName));
    }

    $data = parseXML($fileDoc,$root,$paths);

    return $data;
}
?>
